define(function (require, exports, module) {
  var CPDF_TextObject = require('./Reader_TextObject.js');
  var CPDF_TextPage = require('./Reader_TextPage.js');
  
  FXREADER.PDFData.Text.TextManager = function (pReaderApp) {
	  var _pReaderApp = pReaderApp,
	  	  _pTextPageList = [],    //文本页面对象数组，格式：页面索引  ->  FXREADER.TextSelect.TEXT_PAGE对象
	  	 _TextPageGettingList = [], //Ajax请求标记数字，格式为：页面索引   ->  是否正在请求(true/false)
	  	 _self = this;
	  
	     /**
       * @description 异步获取某页文本页面信息
       * @param {Number} nPageIndex 页面索引（从0开始）
       */
       this.AjaxGetTextPage = function (nPageIndex,isAsync) {
           if (_TextPageGettingList[nPageIndex])
               return;
           var data = { fileid: _pReaderApp.GetFileID(), fname: _pReaderApp.GetFileName(), page: nPageIndex };
           _TextPageGettingList[nPageIndex] = true;
           $.ajax({
               url: FXREADER.Config.Url.GetPageText,
               type: 'POST',
               dataType: 'json',
               data: data,
               async: true,
               success: function (response) {
                   var error = response.error;
                   if (error != 0) {
                       _TextPageGettingList[nPageIndex] = false;
                       return;
                   }
                   try {
                       var jsonTextPageData = $.parseJSON(response.TextPageData);
                       if (jsonTextPageData != null) {
                           var pTextPage = new CPDF_TextPage();
                           pTextPage.Init(jsonTextPageData);
                           _pTextPageList[nPageIndex] = pTextPage;
                       }
                   }
                   catch (ex) {
                	   var pTextPage = new CPDF_TextPage();
                       pTextPage.Init("");
                	   _pTextPageList[nPageIndex] = pTextPage;
                       _TextPageGettingList[nPageIndex] = false;
                       return false;
                   }
               },
               error: function () {
            	   var pTextPage = new CPDF_TextPage();
                   pTextPage.Init("");
            	   _pTextPageList[nPageIndex] = pTextPage;
                   _TextPageGettingList[nPageIndex] = false;
                   return false;
               }
           });
       };
       
       /**
        * @description 获取文本页面对象
        * @private
        * @param {Number} nPageIndex 页面索引（从0开始）
        * @param {Boolean} isAsync 是否异步
        * @return {FXREADER.TextSelect.TEXT_PAGE} 文本页面对象;
        */
        this.GetTextPage = function(nPageIndex,isAsync) {
            var page = _pTextPageList[nPageIndex];
            if (!page) {
                _self.AjaxGetTextPage(nPageIndex,isAsync);
                if(_pTextPageList[nPageIndex])
                	return _pTextPageList[nPageIndex];
                return {text:null,blocking:true};
            }
            return page;
        };
  };
	
	
   return FXREADER.PDFData.Text.TextManager;
});